#pragma once


#pragma warning(disable:4786)
#include <map>
#include "../../Lib_Base/inc/imessage.h"
#include "ISocket.h"
#include "IServe.h"
#include "KernelMsgDef.h"
#include "../../Lib_Base/inc/Timer.h"

namespace ns
{
	static const char* SERVE_NAME = "NET_SERVE";
	enum TYPE{ RECEIVE,SEND};

	class CNetworkServeImpl : public serve::IServe, private net::IServeSocketEvent
	{
	public:
		static  net::IServeSocket*  m_pServeSocket;
		
	public:
		CNetworkServeImpl(serve::IPort* pPort, USHORT usServeInterval,TYPE netType = RECEIVE);
		virtual ~CNetworkServeImpl();

		bool			Init			(int nServePort ,const char* cIP);
	private:
		TYPE            m_Type;
		serve::IPort*	m_pPort;
		USHORT			m_usServeInterval;

		bool            m_InitCatcher;
		void*			m_pExTrans;
	public:
		virtual ULONG		Release(void)				{	delete this; return 0;	}
		virtual const char* GetName(void)		const	{	return SERVE_NAME;	}
		virtual bool		PreCreate(void)				{	return true; }
		virtual bool		Create(void)				{	return true; }
		virtual bool		Run(void)					{	return true; }
		virtual void		PostDestroy(void)			{	return ; }
		virtual void		Destroy(void)				{	return ; }
	
		// Interface of IServe
	public:
		virtual USHORT	GetInterval		(void) const		{ return m_usServeInterval; }
		virtual USHORT	GetPort			(void) const		{ return m_pPort->GetPort(); }
		virtual void	SendMsg			(sbase::IMessage& msg, int nPortDes/*, int nPortDesGroup = serve::PORTGROUP_ALL*/);
		virtual int		OnProcess		(void);
		virtual void    CloseSock(int socket)   { OnCloseSck(socket);return ;};
		virtual int TotalSocket(char type);
		
		// Interface of IServeSocketEvent
	protected:
		virtual int	OnAcceptSck		(SOCKET socket);
		virtual void	OnEstablishSck	(int AutoId,const char* Ip);
		virtual void	OnCloseSck		(SOCKET socket);
		virtual void	OnCloseSck_Iocp		(int AutoId);
		virtual int		OnRcvMsg		(int AutoId, const char* buf, int nLen);
	};
	char * GetHostIP();
	serve::IServe* ServeCreate(serve::IRouter* pRouter, USHORT usInterval,const char* IP, int nListenPort, TYPE netType = RECEIVE);
}